package com.jk.vehicle.core.controller;

import com.jk.vehicle.core.exception.LoginException;
import com.jk.vehicle.core.exception.ServiceException;
import com.jk.vehicle.core.constants.TokenConstants;
import com.jk.vehicle.core.entity.BaseOriginEntity;
import com.jk.vehicle.core.entity.ResponseEntity;
import com.jk.vehicle.core.service.impl.AbstractBizService;
import com.jk.vehicle.core.utils.Query;
import com.jk.vehicle.core.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 持久化controller
 *
 * @author KangJian
 * @date 2018/6/15
 */
@Slf4j
@RestController
public class PersistController<Biz extends AbstractBizService, Entity extends BaseOriginEntity> extends BaseController {

    /**
     * request
     */
    @Autowired
    protected HttpServletRequest request;
    /**
     * 基础服务抽象类
     */
    @Autowired
    protected Biz baseBiz;

    /**
     * 校验是否是登陆并且管理员账号
     *
     * @return
     */
    protected boolean checkLoginAndAdmin() {
        String token = request.getHeader(TokenConstants.TOKEN_HEADER);
        try {
            if (TokenUtil.verifyToken(token).get(TokenConstants.LOGIN_NAME).asString().equals(SUPPER_ADMIN)) {
                // 在redis中清除已登陆
                return true;
            }
        } catch (Exception e) {
            throw new LoginException("token解析错误，请登陆获取token");
        }
        return false;
    }

    /**
     * 新增
     * @param entity
     * @param result
     * @return
     */
    @PostMapping
    public ResponseEntity add(@RequestBody @Valid Entity entity, BindingResult result) {
        checkBindingResult(result);
        baseBiz.insertSelective(entity);
        return successResult();
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity get(@PathVariable int id) {
        return successResult(baseBiz.selectById(id));
    }

    /**
     * 修改
     * @param entity
     * @param result
     * @return
     */
    @PutMapping("/{id}")
    public ResponseEntity update(@RequestBody @Valid Entity entity, BindingResult result) {
        checkBindingResult(result);
        if (entity == null || entity.getId() == null) {
            throw new ServiceException("更新id不能为空");
        }
        baseBiz.updateSelectiveById(entity);
        return successResult();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity remove(@PathVariable int id) {
        baseBiz.deleteById(id);
        return successResult();
    }

    /**
     * 列表
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity all() {
        return successResult(baseBiz.selectListAll());
    }

    /**
     * 分页
     * @param params
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        return successResult(baseBiz.selectByQuery(query));
    }
}
